#include "Backup.h"
#include "mainwindow.h"

#include <QtGui>

void Backup::initializeVariables(QStringList backupList, QString backupPath) {
    this->backupList = backupList;
    this->backupPath = backupPath;
}

void Backup::run()
{
    // Error-checking finished
    // Back up everything from the backupList
    //
    // Initialize our statistics first
    numFoldersCopied = 0;
    numFilesCopied = 0;

    for ( int i = 0; i < backupList.count(); i++ ) {

        QString listItemText = backupList.at(i);

        if (MainWindow::isDirectory(listItemText) == true) {

            QDir temp(listItemText);
            temp.cdUp();

            copyFolder(temp.path(), listItemText);
        }
        else {
            copyFile(listItemText, backupPath + MainWindow::onlyFileName(listItemText));
        }
    }

    emit doneBackingUp(numFoldersCopied, numFilesCopied);
}


void Backup::copyFile(QString file, QString dest)
{
    // "pathInfo" allows us to grab only the file name and store it in "justFileName"
    QFileInfo pathInfo(file);
    QString justFileName = pathInfo.fileName();
    bool copied = false;

    // tries to copy the file. This will return false if the file already exists
    // this will not overwrite the file on the destination path either
    if( !QFile::copy(file, dest) ) {

        // checks to see if the last modified time on the source file is greater than the one
        // on the destination file. If it is, it has been modified and needs to be copied
        if ( MainWindow::fileLastModified(file) > MainWindow::fileLastModified(dest) ) {

            // removes the old copy
            if ( (QFile::remove(dest))) {

                //finally copies the file because it has been modified if removing the old one doesn't fail
                QFile::copy(file, dest);
                //addToConsole(justFileName + " updated");
                copied = true;
            }

            else {
                //addToConsole("Error: removing " + dest + " failed so we can't update it");
            }
        }

        else {
            //addToConsole(justFileName + " is unmodified");
        }
    }
    else {
        copied = true;
       //addToConsole(justFileName + " backed up");
    }

    if ( copied ) {
        numFilesCopied++;
        QFile size(file);
        emit copiedBytes(size.size());
    }

}

void Backup::copyFolder(QString source, QString path) {

    if ( !path.endsWith("/") ) {
        path.append("/");
    }

    if ( !source.endsWith("/") ) {
        source.append("/");
    }

    // We need to maintain directory structure, so we cut off the source from the beginning
    // of the directory. The result is just the end of the path (so "pics/summer/" would turn into "summer/" if source is "pics/")
    QString endOfPath(path);
    endOfPath.replace(source, "");
    QDir backupDir(backupPath + endOfPath);
    if ( !backupDir.exists() ) {

        backupDir.mkpath(backupDir.path());
        numFoldersCopied++;
    }

    // Now that the destination is set up, go through the source and copy all the files and folders
    QDir dir(path);

    // Copy files
    QStringList fileEntries = dir.entryList(QDir::Files | QDir::Hidden);

    foreach(QString fileEntry, fileEntries) {
        copyFile(path + fileEntry, backupDir.path() + "/" + fileEntry);
    }

    // Copy folders
    QStringList dirEntries = dir.entryList(QDir::NoDotAndDotDot | QDir::Dirs | QDir::Hidden);

    foreach(QString dirEntry, dirEntries) {
        copyFolder(source, path + dirEntry);
    }
}
